﻿using BLL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MachineDemo
{
    public partial class ConnectionSetForm : Form
    {
        DeviceConnMessage mod_DriveMessage;
        public ConnectionSetForm()
        {
            InitializeComponent();
            dataGV_DriveItemMessage.AutoGenerateColumns = false;//表格取消自动更新列

            dataGV_DriveItemMessage.DataSource = LogicSingle.Instaince._DeviceConnList;//表格更新数据源

            cbx_ConnMode.DataSource = Enum.GetNames(typeof(ConnectWay));//连接方式
            cbx_ComParity.DataSource = Enum.GetNames(typeof(Parity));//奇偶校验
            cbx_ComStopBits.DataSource = Enum.GetNames(typeof(StopBits));//停止位
            cbx_ComMessageMode.DataSource = Enum.GetNames(typeof(ComMessageMode));
        }

        #region 表格双击获取信息
        /// <summary>
        /// 表格双击获取信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGV_DriveItemMessage_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGV_DriveItemMessage.CurrentRow != null)//当前Datagridview行数不能为空
            {
                string studentId = this.dataGV_DriveItemMessage.CurrentRow.Cells["Col_DeviceName"].Value.ToString();
                mod_DriveMessage = LogicSingle.Instaince._DeviceConnList.Where(x => x.DeviceName == studentId).FirstOrDefault();
                txb_DriveName.Text = mod_DriveMessage.DeviceName!=null?mod_DriveMessage.DeviceName.ToString():"";
                txb_DriveIp.Text = mod_DriveMessage.ServerAdress!=null? mod_DriveMessage.ServerAdress.ToString():"";
                txb_DrivePort.Text = mod_DriveMessage.ServerPort.ToString();
                txb_DriveBindIp.Text= mod_DriveMessage.BindAdress!=null?mod_DriveMessage.BindAdress.ToString():"";
                txb_DriveBindPort.Text = mod_DriveMessage.BindPort.ToString();
                cbx_ConnMode.SelectedIndex = cbx_ConnMode.FindString(mod_DriveMessage.ConnMode.ToString());
                txb_DriveComName.Text = mod_DriveMessage.PortName!=null? mod_DriveMessage.PortName.ToString():"";
                txb_DriveBaudRate.Text = mod_DriveMessage.BaudRate.ToString();
                cbx_ComParity.SelectedIndex = cbx_ComParity.FindString(mod_DriveMessage.Parity.ToString());
                cbx_ComStopBits.SelectedIndex = cbx_ComStopBits.FindString(mod_DriveMessage.StopBits.ToString());
                cbx_ComMessageMode.Text = mod_DriveMessage.ComMessageMode.ToString();
                txb_ComBitLenght.Text = mod_DriveMessage.DataBits.ToString();
            }
        }
        #endregion

        #region 接收模式切换修改样式
        private void cbx_ConnMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Silver
            //White
            switch (((ComboBox)sender).SelectedItem)
            {
                case "Tcp":
                    txb_DriveComName.FillColor = Color.Silver;
                    txb_ComBitLenght.FillColor = Color.Silver;
                    txb_DriveBaudRate.FillColor = Color.Silver;
                    cbx_ComParity.BackColor = Color.Silver;
                    cbx_ComStopBits.BackColor = Color.Silver;
                    cbx_ComMessageMode.BackColor = Color.Silver;
                    txb_DriveIp.FillColor = Color.White;
                    txb_DrivePort.FillColor= Color.White;
                    txb_DriveBindIp.FillColor= Color.White;
                    txb_DriveBindPort.FillColor= Color.White;
                    break;
                case "Com":
                    txb_DriveComName.FillColor = Color.White;
                    txb_ComBitLenght.FillColor = Color.White;
                    txb_DriveBaudRate.FillColor = Color.White;
                    cbx_ComParity.BackColor = Color.White;
                    cbx_ComStopBits.BackColor = Color.White;
                    cbx_ComMessageMode.BackColor = Color.White;
                    txb_DriveIp.FillColor = Color.Silver;
                    txb_DrivePort.FillColor = Color.Silver;
                    txb_DriveBindIp.FillColor = Color.Silver;
                    txb_DriveBindPort.FillColor = Color.Silver;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 修改设备信息
        private void btn_SaveDriveMessage_Click(object sender, EventArgs e)
        {
            foreach (var item in LogicSingle.Instaince._DeviceConnList)
            {
                if (item.DeviceName== txb_DriveName.Text)
                {
                    try
                    {
                        item.DeviceName = txb_DriveName.Text;
                        item.ConnMode = (ConnectWay)Enum.Parse(typeof(ConnectWay), this.cbx_ConnMode.Text, true);
                        item.ServerAdress = txb_DriveIp.Text;
                        item.ServerPort = txb_DrivePort.Text != "" ? Convert.ToInt32(txb_DrivePort.Text) : 0;
                        item.BindAdress = txb_DriveBindIp.Text;
                        item.BindPort = txb_DriveBindPort.Text != "" ? Convert.ToInt32(txb_DriveBindPort.Text) : 0;
                        item.PortName = txb_DrivePort.Text;
                        item.BaudRate = txb_DriveBaudRate.Text != "" ? Convert.ToInt32(txb_DriveBaudRate.Text) : 0;
                        item.DataBits = txb_ComBitLenght.Text != "" ? Convert.ToInt32(txb_ComBitLenght.Text) : 0;
                        item.Parity = (Parity)Enum.Parse(typeof(Parity), this.cbx_ComParity.Text, true);
                        item.StopBits = (StopBits)Enum.Parse(typeof(StopBits), this.cbx_ComStopBits.Text, true);
                        item.ComMessageMode = (ComMessageMode)Enum.Parse(typeof(ComMessageMode), cbx_ComMessageMode.Text, true); ;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("修改失败");
                        return;
                    }

                    MessageBox.Show("修改成功");
                    return;
                }
            }
            MessageBox.Show("修改项不存在");
        }
        #endregion
    }
}
